###############################################
### Project: Communicating in an eventful   ###
###          campaign                       ###
### Task:    Preparation - Poll data        ###
### Title:   05_Preparation_Polls.R         ###
###############################################

#--------------------------------------------------------------------------------------------------
# Description:
#
# This script prepares poll data for further use during the analysis. The data set covers the
# months of July, August and September 2021. The variable "poll" stores the mean values for all
# polls conducted during the respective month.
#
# Source: https://de.wikipedia.org/wiki/Bundestagswahl_2021/Umfragen_und_Prognosen#%C3%9Cbersicht)
#--------------------------------------------------------------------------------------------------

#---------------------------------------------------------------------------------------------------------------------

# Collect election results

df_may21 <- data.frame(party = c("CDU/CSU", "SPD", "AfD", "FDP", "Linke", "Grüne"),
                       month = "May",
                       poll = c(24.7, 15.0, 11.1, 11.7, 6.9, 24.4),
                       elec_17 = c(32.9, 20.5, 12.6, 10.7, 9.2, 8.9))

df_jun21 <- data.frame(party = c("CDU/CSU", "SPD", "AfD", "FDP", "Linke", "Grüne"),
                       month = "June",
                       poll = c(27.6, 15.5, 10.4, 12.4, 6.8, 20.6),
                       elec_17 = c(32.9, 20.5, 12.6, 10.7, 9.2, 8.9))

df_jul21 <- data.frame(party = c("CDU/CSU", "SPD", "AfD", "FDP", "Linke", "Grüne"),
                       month = "July",
                       poll = c(28.6, 16.0, 10.6, 11.8, 7.0, 18.7),
                       elec_17 = c(32.9, 20.5, 12.6, 10.7, 9.2, 8.9))

df_aug21 <- data.frame(party = c("CDU/CSU", "SPD", "AfD", "FDP", "Linke", "Grüne"),
                       month = "August",
                       poll = c(23.9, 20.7, 10.9,	12.1,	6.7, 18.3),
                       elec_17 = c(32.9, 20.5, 12.6, 10.7, 9.2, 8.9))

df_sep21 <- data.frame(party = c("CDU/CSU", "SPD", "AfD", "FDP", "Linke", "Grüne"),
                       month = "September",
                       poll = c(21.4,	25.5,	11.2,	11.6,	6.4,	16.0),
                       elec_17 = c(32.9, 20.5, 12.6, 10.7, 9.2, 8.9))

df <- rbind(df_may21, df_jun21, df_jul21, df_aug21, df_sep21)

#---------------------------------------------------------------------------------------------------------------------

# Calculate poll trend (trend of polls compared to previous month)

## poll trend for current month
poll_trend <- c()

for(i in 1:nrow(df)) {
  
  party_tmp <- df$party[i]
  month_tmp <- df$month[i]
  
  if(df$month[i] == "May") {
    res <- NA
  }
  
  if(df$month[i] == "June") {
    res <- df$poll[df$party == party_tmp & df$month == "June"] -
      df$poll[df$party == party_tmp & df$month == "May"]
  }
  
  if(df$month[i] == "July") {
    res <- df$poll[df$party == party_tmp & df$month == "July"] -
      df$poll[df$party == party_tmp & df$month == "June"]
  }
  
  if(df$month[i] == "August") {
    res <- df$poll[df$party == party_tmp & df$month == "August"] -
      df$poll[df$party == party_tmp & df$month == "July"]
  }
  
  if(df$month[i] == "September") {
    res <- df$poll[df$party == party_tmp & df$month == "September"] -
      df$poll[df$party == party_tmp & df$month == "August"]
  }
  
  poll_trend <- c(poll_trend, res)
}

df$poll_trend <- poll_trend

df <- filter(df, month != "May")

#---------------------------------------------------------------------------------------------------------------------

# Save final data set

saveRDS(df, "Poll_Data.RDS")

